package xhwl.insurance.ui;

import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import xhwl.insurance.R;
import xhwl.insurance.BaseActivity;
import xhwl.insurance.customView.ProgressWebView;

public class WebViewActivity extends BaseActivity {
    public static final String EXTRA_URL = "extra_url";
    public static final String EXTRA_TITLE = "extra_title";
    private ProgressWebView webview;
    String url;
    String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        title = getIntent().getStringExtra(EXTRA_TITLE);
        url = getIntent().getStringExtra(EXTRA_URL);
        setContentView(R.layout.activity_webview);
    }

    @Override
    public void initViews() {
        // TODO Auto-generated method stub
        initTitleViews();
        webview = (ProgressWebView) findViewById(R.id.webview);
        webview.getSettings().setDefaultTextEncodingName("UTF-8");
        webview.getSettings().setDomStorageEnabled(true);
        webview.getSettings().setJavaScriptEnabled(true);
    }

    @Override
    protected void setUpTitleView() {
        // TODO Auto-generated method stub
        titleTextView.setText(title);
    }

    public void initListener() {
        // TODO Auto-generated method stub
        webview.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                System.out.println(url);
                view.loadUrl(url);
                return true;
            }
        });
    }

    public void loadData() {
        // TODO Auto-generated method stub
        webview.loadUrl(url);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack(); // goBack()表示返回WebView的上一页面
            return true;
        }
        WebViewActivity.this.finish();
        return false;

    }

}
